//
//  NHBaseTableView.m
//  LZNeiHan
//
//  Created by 梁梓烽 on 16/10/1.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "NHBaseTableView.h"

@implementation NHBaseTableView


#pragma mark -构造方法
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        self.tableFooterView = [UIView new];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark -其他

-(void)nh_updateWithUpdateBlock:(void(^)(NHBaseTableView *tableView))updateBlock
{
    if (updateBlock) {
        [self beginUpdates];
        updateBlock(self);
        [self endUpdates];
    }
}

-(UITableViewCell * )nh_cellAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath) return nil;
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    NSInteger sectionNumber = self.numberOfSections;
    NSInteger rowNumber  = [self numberOfRowsInSection:section];
    
    if (section + 1> sectionNumber || section < 0) {
         NSLog(@"刷新section: %ld 已经越界, 总组数: %ld", section, sectionNumber);
        return nil;
    }
    else if (row +1 >rowNumber || row<0)
    {
        NSLog(@"刷新row: %ld 已经越界, 总行数: %ld 所在section: %ld", indexPath.row, rowNumber, section);
        return nil;
    }
    return [self cellForRowAtIndexPath:indexPath];
}

#pragma mark -注册
/** 注册普通tableviewcell */
-(void)nh_registerCellClass:(Class)cellClass identifier:(NSString *)identifier
{
    if (cellClass && identifier.length) {
        
        [self registerClass:cellClass forCellReuseIdentifier:identifier];
      
    }

}

/** 注册从xib中加载的一个tableviewcell */
-(void)nh_registerCellNib:(Class)cellNib identifier:(NSString *)identifier
{
    
    if (cellNib && identifier.length) {
        [self registerNib:[UINib nibWithNibName:[cellNib description] bundle:nil] forCellReuseIdentifier:identifier];
    }
}

/** 注册一个普通的UITableViewHeaderFooterView*/

-(void)nh_registerHeaderFooterClass:(Class)headerFooterClass identifier:(NSString *)identifier
{
    if (headerFooterClass  && identifier.length) {
        
        [self registerClass:headerFooterClass forHeaderFooterViewReuseIdentifier:identifier];
    }
}

/** xib注册一个普通的UITableViewHeaderFooterView*/
-(void)nj_registerHeaderFooter:(Class)headerFooterNib identifier:(NSString *)identifier
{
    if (headerFooterNib && identifier.length) {
        [self registerNib:[UINib nibWithNibName:[headerFooterNib description] bundle:nil] forHeaderFooterViewReuseIdentifier:identifier];
    }

}


#pragma mark - 只对已经存在的cell进行刷新，没有类似于系统的 如果行不存在，默认insert操作

/** 刷新单行、动画默认*/
- (void)nh_reloadSingleRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self  nh_reloadSingleRowAtIndexPath:indexPath animation:None];
}
/** 刷新单行、动画默认*/
-(void)nh_reloadSingleRowAtIndexPath:(NSIndexPath *)indexPath  animation:(NHBaseTableViewRowAnimation)animation
{
    if (!indexPath) return;
    
    NSInteger section =  indexPath.section;
    NSInteger  row = indexPath.row;
    NSInteger sectionNumber = self.numberOfSections;
    NSInteger rowNumber = [self numberOfRowsInSection:section];
    
    if (indexPath.section  +1  > sectionNumber || indexPath.section<0) {
         NSLog(@"刷新section: %ld 已经越界, 总组数: %ld", indexPath.section, sectionNumber);
    }
    else if (indexPath.row +1 > rowNumber || indexPath.row<0)
    {
        
        NSLog(@"刷新row: %ld 已经越界, 总行数: %ld 所在section: %ld", indexPath.row, rowNumber, section);

    }
    
    else
    {
        [self beginUpdates];
        [self  reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimation)animation];
        [self endUpdates];
    }
}

/** 刷新多行、动画默认*/
- (void)nh_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
    [self nh_reloadRowsAtIndexPaths:indexPaths animation:None];
}

/** 刷新多行、动画默认*/
- (void)nh_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths animation:(NHBaseTableViewRowAnimation)animation
{
     if (!indexPaths.count)  return;
    
    WeakSelf(a)
    
    [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj isKindOfClass:[NSIndexPath class]]) {
            [a nh_reloadSingleRowAtIndexPath:obj animation:animation];
        }
    }];

}


/** 刷新某个section、动画默认*/
- (void)nh_reloadSingleSection:(NSInteger)section

{
    [self nh_reloadSingleSection:section animation:None];
}

/** 刷新某个section、动画自定义*/
- (void)nh_reloadSingleSection:(NSInteger)section animation:(NHBaseTableViewRowAnimation)animation
{
    if (!section) return;
    
    NSInteger sectionNumber =  self.numberOfSections;
    
    if (section +1 > sectionNumber) {
        NSLog(@"刷新section: %ld 已经越界, 总组数: %ld", section, sectionNumber);
    }
    else {
        
        [self beginUpdates];
        [self reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:(UITableViewRowAnimation)animation];
        
        [self endUpdates];
    }
    

}

/** 刷新多个section、动画默认*/
- (void)nh_reloadSections:(NSArray <NSNumber *>*)sections
{
    [self nh_reloadSections:sections animation:None];
}

/** 刷新多个section、动画自定义*/
- (void)nh_reloadSections:(NSArray <NSNumber *>*)sections animation:(NHBaseTableViewRowAnimation)animation
{
    if (!sections.count) return;
    
    WeakSelf(weakself)
    
    [sections enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSNumber class]]) {
            [weakself  nh_reloadSingleSection:obj.integerValue animation:animation];
        }
    }];
}

#pragma mark - 对cell进行删除操作
/** 删除单行、动画默认*/
- (void)nh_deleteSingleRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self nh_deleteSingleRowAtIndexPath:indexPath animation:None];
}

/** 删除单行、动画自定义*/
- (void)nh_deleteSingleRowAtIndexPath:(NSIndexPath *)indexPath animation:(NHBaseTableViewRowAnimation)animation
{
    
    //数组为判断
    if (!indexPath) return;
    
    
    //section row 是否越界判断
    NSInteger section = indexPath.section;
    NSInteger row  = indexPath.row;
    NSInteger sectionNumber = self.numberOfSections;
    NSInteger rowNumber = [self numberOfRowsInSection:section];
    
    if (section +1 >sectionNumber || section < 0) {
        
        NSLog(@"删除section: %ld 已经越界, 总组数: %ld", indexPath.section, sectionNumber);
    }
    
    else if (row + 1 > rowNumber || row < 0)
    {
        
        NSLog(@"删除row: %ld 已经越界, 总行数: %ld 所在section: %ld", indexPath.row, rowNumber, section);
    }
    else
        
    {
        [self beginUpdates];
        
        [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimation)animation];
        
        [self endUpdates];
    }
    

    
}

/** 删除多行、动画默认*/
- (void)nh_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
    [self nh_deleteRowsAtIndexPaths:indexPaths animation:None];
}

/** 删除多行、动画自定义*/
- (void)nh_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths animation:(NHBaseTableViewRowAnimation)animation
{
    if (!indexPaths.count) return;
    
    WeakSelf(weakself)
    [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSIndexPath class]]) {
        
            [weakself nh_deleteSingleRowAtIndexPath:obj animation:animation];
        }
    }];
}

/** 删除某个section、动画默认*/
- (void)nh_deleteSingleSection:(NSInteger)section
{
    [self nh_deleteSingleSection:section animation:None];
}

/** 删除某个section、动画自定义*/
- (void)nh_deleteSingleSection:(NSInteger)section animation:(NHBaseTableViewRowAnimation)animation
{
    if (!section) return;
    
    NSInteger sectionNumber  = self.numberOfSections;
    
    if (section + 1 > sectionNumber) {
           NSLog(@"刷新section: %ld 已经越界, 总组数: %ld", section, sectionNumber);
    }
    
    else
    {
        [self beginUpdates];
        [self deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:(UITableViewRowAnimation)animation];
        [self endUpdates];
    }
    
}

/** 删除多个section*/
- (void)nh_deleteSections:(NSArray <NSNumber *>*)sections
{
    [self nh_deleteSections:sections animation:None];
}

/** 删除多个section*/
- (void)nh_deleteSections:(NSArray <NSNumber *>*)sections animation:(NHBaseTableViewRowAnimation)animation

{
    if (!sections.count) return;
    
    WeakSelf(weakself)
    
    [sections enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ( [obj isKindOfClass:[NSNumber class]]) {
            [weakself  nh_deleteSingleSection:obj.integerValue animation:animation];
        }
    }];
}


#pragma mark - 对cell进行插入操作
/** 增加单行 动画无*/
- (void)nh_insertSingleRowAtIndexPath:(NSIndexPath *)indexPath

{
    [self nh_insertSingleRowAtIndexPath:indexPath animation:None];
}

/** 增加单行，动画自定义*/
- (void)nh_insertSingleRowAtIndexPath:(NSIndexPath *)indexPath animation:(NHBaseTableViewRowAnimation)animation

{
    //section row 是否越界判断
    NSInteger section = indexPath.section;
    NSInteger row  = indexPath.row;
    NSInteger sectionNumber = self.numberOfSections;
    NSInteger rowNumber = [self numberOfRowsInSection:section];
    
    if (section +1 >sectionNumber || section < 0) {
        
        NSLog(@"删除section: %ld 已经越界, 总组数: %ld", indexPath.section, sectionNumber);
    }
    
    else if (row + 1 > rowNumber || row < 0)
    {
        
        NSLog(@"删除row: %ld 已经越界, 总行数: %ld 所在section: %ld", indexPath.row, rowNumber, section);
    }
    else
        
    {
        [self beginUpdates];
        
        [self insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimation)animation];

        [self endUpdates];
    }
    
   }

/** 增加单section，动画无*/
- (void)nh_insertSingleSection:(NSInteger)section
{
    [self nh_insertSingleSection:section animation:None];
}

/** 增加单section，动画自定义*/
- (void)nh_insertSingleSection:(NSInteger)section animation:(NHBaseTableViewRowAnimation)animation
{
    if (!section) return;
    
    NSInteger sectionNumber  = self.numberOfSections;
    
    if (section + 1> sectionNumber) {
        NSLog(@"刷新section: %ld 已经越界, 总组数: %ld", section, sectionNumber);
    }
    else
        
    {
        [self insertSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:(UITableViewRowAnimation)animation];
    }


}

/** 增加多行，动画无*/
- (void)nh_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
    [self nh_insertRowsAtIndexPaths:indexPaths animation:None];
}

/** 增加多行，动画自定义*/
- (void)nh_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths animation:(NHBaseTableViewRowAnimation)animation
{
    if (!indexPaths.count)  return;
    
    WeakSelf(weakself)
    [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSIndexPath class]]) {
            [weakself nh_insertSingleRowAtIndexPath:obj animation:animation];
        }
    }];
}

/** 增加多section，动画无*/
- (void)nh_insertSections:(NSArray <NSNumber *>*)sections
{
    [self nh_insertSections:sections animation:None];
}

/** 增加多section，动画自定义*/
- (void)nh_insertSections:(NSArray <NSNumber *>*)sections animation:(NHBaseTableViewRowAnimation)animation
{
    if (!sections.count) return;
    
    WeakSelf(weakself)
    
    [sections enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[NSNumber class]]) {
            [self nh_insertSingleSection:obj.integerValue animation:animation];
        }
    }];
}


@end
